import sqlite3
from models.game import Game
from models.review import Review


# Creates the initial database and populates it with games
def main():
    conn = sqlite3.connect("database.db")
    c = conn.cursor()

    # Create the table
    c.execute("DROP TABLE IF EXISTS games")
    c.execute('''
        CREATE TABLE games (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            title TEXT,
            description TEXT,
            author TEXT,
            localVersion INTEGER,
            remoteVersion INTEGER
        )
        ''')
    c.execute("DROP TABLE IF EXISTS reviews")
    c.execute('''
        CREATE TABLE reviews (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            game_id INTEGER,
            author TEXT,
            rating INTEGER,
            body TEXT
        )
        ''')
    conn.commit()

    # Insert mock games
    for game in Game.getMocks():
        c.execute("""INSERT INTO games
                  (title, description, author, localVersion, remoteVersion)
                  VALUES (?, ?, ?, ?, ?)""",
                  (game.getTitle(), game.getDescription(), game.getAuthor(),
                   game.getLocalVersion(), game.getRemoteVersion()))
    conn.commit()

    # Insert mock reviews
    for review in Review.getMocks():
        c.execute("""INSERT INTO reviews
                  (game_id, author, rating, body)
                  VALUES (?, ?, ?, ?)""",
                  (review.getGameId(), review.getAuthor(),
                   review.getRating(), review.getBody()))
    conn.commit()

    # Close connection
    conn.close()

if __name__ == "__main__":
    main()
